// Android权限查询
function requestAndroidPermission(permissionID) {
    return new Promise((resolve, reject) => {
        plus.android.requestPermissions(
            [permissionID], // 理论上支持多个权限同时查询，但实际上本函数封装只处理了一个权限的情况。有需要的可自行扩展封装
            function (resultObj) {
                var result = 0;
                for (var i = 0; i < resultObj.granted.length; i++) {
                    var grantedPermission = resultObj.granted[i];
                    console.log('已获取的权限：' + grantedPermission);
                    result = 1
                }
                for (var i = 0; i < resultObj.deniedPresent.length; i++) {
                    var deniedPresentPermission = resultObj.deniedPresent[i];
                    console.log('拒绝本次申请的权限：' + deniedPresentPermission);
                    result = 0
                }
                for (var i = 0; i < resultObj.deniedAlways.length; i++) {
                    var deniedAlwaysPermission = resultObj.deniedAlways[i];
                    console.log('永久拒绝申请的权限：' + deniedAlwaysPermission);
                    result = -1
                }
                resolve(result);
                // 若所需权限被拒绝,则打开APP设置界面,可以在APP设置界面打开相应权限
                // if (result != 1) {
                // gotoAppPermissionSetting()
                // }
            },
            function (error) {
                console.log('申请权限错误：' + error.code + " = " + error.message);
                resolve({
                    code: error.code,
                    message: error.message
                });
            }
        );
    });
}
// utils/permission.js
export async function requestWifiPermission() {
    const WIFI_PERMISSION = 'android.permission.ACCESS_WIFI_STATE';
    
    try {
        // 首先检查是否已有权限
        const hasPermission = await checkPermission(WIFI_PERMISSION);
        if (hasPermission) {
            return { granted: true };
        }
        
        // 请求权限
        const result = await requestAndroidPermission(WIFI_PERMISSION);
        
        // 如果用户拒绝，可以引导到设置页面
        if (!result.granted && result.deniedType === 'permanent') {
            console.log('建议引导用户到应用设置中开启权限');
            // 可以调用以下函数（需要实现）
            // gotoAppPermissionSetting();
        }
        
        return result;
    } catch (error) {
        console.error('请求WiFi权限失败:', error);
        return { granted: false, error };
    }
}

// 检查权限状态的辅助函数
export async function checkPermission(permissionID) {
    return new Promise((resolve) => {
        plus.android.checkPermission(permissionID, (granted) => {
            resolve(granted);
        }, (error) => {
            console.error('检查权限失败:', error);
            resolve(false);
        });
    });
}
var isIos
// #ifdef APP-PLUS
isIos = (plus.os.name == "iOS")
// #endif
// 判断定位权限是否开启
export async function judgeIosPermissionLocation() {
	var result = false;
	var cllocationManger = plus.ios.import("CLLocationManager");
	var status = cllocationManger.authorizationStatus();
	result = (status != 2)
	console.log("定位权限开启：" + result);
	// 以下代码判断了手机设备的定位是否关闭，推荐另行使用方法 checkSystemEnableLocation
	var enable = cllocationManger.locationServicesEnabled();
	var status = cllocationManger.authorizationStatus();
	console.log("enable:" + enable);
	console.log("status:" + status);
	if (enable && status != 2) {
		result = true;
		console.log("手机定位服务已开启且已授予定位权限");
	} else {
		console.log("手机系统的定位没有打开或未给予定位权限");
	}
	plus.ios.deleteObject(cllocationManger);
	return result;
}
export default {
    requestAndroidPermission,
    requestWifiPermission,
    judgeIosPermissionLocation,
}